package com.suning.sawp.service.impl.contend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.common.RecordList;
import com.suning.sawp.po.contend.CommBean;
import com.suning.sawp.po.contend.CommPersonDTO;
import com.suning.sawp.po.contend.NmLibBean;
import com.suning.sawp.po.contend.PersonBean;
import com.suning.sawp.po.contend.TopicBean;
import com.suning.sawp.po.contend.TopicVoteDTO;
import com.suning.sawp.po.contend.VoteTopicRel;
import com.suning.sawp.po.contend.WxUserInfo;
import com.suning.sawp.service.util.DateUtils;
import com.vdurmont.emoji.EmojiParser;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 12061748
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class ContendService {
    @Resource
    DalClient dalClient;

    /**
     * 
     * 功能描述:查询历史话题 <br>
     * 〈功能详细描述〉
     *
     * @param start
     * @param limit
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<TopicBean> queryTopicList(Integer start, Integer limit, String topicName) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", start);
        map.put("limit", limit);
        if (StringUtils.isNotBlank(topicName)) {
            map.put("topicName", "%" + topicName + "%");
        }
        return dalClient.queryForList("contend.queryTopicList", map, TopicBean.class);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param topicName
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Long countTopicList(String topicName) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(topicName)) {
            map.put("topicName", topicName);
        }
        Map<String, Object> result = dalClient.queryForMap("contend.countTopicList", map);
        return (Long) result.get("num");
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param topicBean
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void saveTopic(TopicBean topicBean) {
        if (topicBean.getTopicId() == null) {
            topicBean.setSupNum(0);
            topicBean.setRejNum(0);
            topicBean.setRejCommNum(0);
            topicBean.setSupCommNum(0);
            topicBean.setCreateTime(DateUtils.getNowLongStr());
            dalClient.persist(topicBean);
        } else {
            topicBean.setSupNum(null);
            topicBean.setRejNum(null);
            topicBean.setRejCommNum(null);
            topicBean.setSupCommNum(null);
            dalClient.dynamicMerge(topicBean);
        }
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param topicBean
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void deleteTopic(String topicId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("topicId", topicId);
        dalClient.execute("contend.deleteTopic", map);
        dalClient.execute("contend.delComm", map);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param topicId
     * @param personId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public TopicVoteDTO queryTopicDetail(String topicId, Long personId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("topicId", topicId);
        if (personId != null) {
            map.put("personId", personId);
            return dalClient.queryForObject("contend.queryMyTopicDetail", map, TopicVoteDTO.class);
        }
        TopicBean topic = dalClient.queryForObject("contend.queryTopicDetail", map, TopicBean.class);
        TopicVoteDTO tv = new TopicVoteDTO();
        BeanUtils.copyProperties(topic, tv);
        return tv;
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param openId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public PersonBean queryWxPersonBeanInfo(WxUserInfo wxUser) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("openId", wxUser.getOpenId());
        PersonBean person = dalClient.queryForObject("contend.queryPersonBeanInfo", map, PersonBean.class);
        if (person == null || person.getPersonId() == null) {
            // 创建一个用户
            person = new PersonBean();
            person.setHeadPic(wxUser.getHeadPic());
            person.setPersonName(EmojiParser.removeAllEmojis(wxUser.getNickname()));
            person.setOpenId(wxUser.getOpenId());
            Long personId = (Long) dalClient.persist(person);
            person.setPersonId(personId);
        }
        return person;
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param wxUser
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public PersonBean queryWxPersonInfo(WxUserInfo wxUser) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("openId", wxUser.getOpenId());
        return dalClient.queryForObject("contend.queryPersonBeanInfo", map, PersonBean.class);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param staffCode
     * @param staffName
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public PersonBean querySoaPersonBeanInfo(String staffCode, String staffName) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("staffCode", staffCode);
        PersonBean person = dalClient.queryForObject("contend.queryPersonBeanInfo", map, PersonBean.class);
        if (person == null || person.getPersonId() == null) {
            // 创建一个用户
            person = new PersonBean();
            Map<String, Object> pmap = new HashMap<String, Object>();
            pmap.put("type", NmLibBean.TypeConstants.TYPE_HEADPIC);
            NmLibBean nmBean = dalClient.queryForObject("contend.getNmInfoEx", pmap, NmLibBean.class);
            // 随机分配一条头像
            person.setHeadPic(nmBean == null ? "" : nmBean.getName());
            person.setPersonName(staffName);
            person.setStaffCode(staffCode);
            Long personId = (Long) dalClient.persist(person);
            person.setPersonId(personId);
        }
        return person;
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param staffCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public PersonBean querySoaPersonInfo(String staffCode) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("staffCode", staffCode);
        return dalClient.queryForObject("contend.queryPersonBeanInfo", map, PersonBean.class);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param openId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public RecordList<CommPersonDTO> queryCommList(Integer start, Integer limit, String topicId, String personId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("personId", personId);
        map.put("topicId", topicId);
        map.put("start", start);
        map.put("limit", limit);
        map.put("isBest", false);
        List<CommPersonDTO> commList = dalClient.queryForList("contend.queryCommList", map, CommPersonDTO.class);
        Map<String, Object> countMap = dalClient.queryForMap("contend.countCommList", map);
        int count = ((Long) countMap.get("num")).intValue();
        return new RecordList<CommPersonDTO>(commList, count);
    }

    public RecordList<CommPersonDTO> queryBackCommList(Integer start, Integer limit, String topicName,
            String commContent) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(topicName)) {
            map.put("topicName", "%" + topicName + "%");
        }
        if (StringUtils.isNotBlank(commContent)) {
            map.put("commContent", "%" + commContent + "%");
        }
        map.put("start", start);
        map.put("limit", limit);
        List<CommPersonDTO> commList = dalClient.queryForList("contend.queryBackCommList", map, CommPersonDTO.class);
        Map<String, Object> countMap = dalClient.queryForMap("contend.countBackCommList", map);
        int count = ((Long) countMap.get("num")).intValue();
        return new RecordList<CommPersonDTO>(commList, count);
    }

    /**
     * 
     * 功能描述:查询最强声 <br>
     * 〈功能详细描述〉
     *
     * @param topicId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CommPersonDTO> queryBestCommList(String topicId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("topicId", topicId);
        map.put("isBest", true);
        return dalClient.queryForList("contend.queryBestCommList", map, CommPersonDTO.class);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param commBean
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void saveComment(CommBean commBean) {
        commBean.setCreateTime(DateUtils.getNowLongStr());
        commBean.setZanNum(0);
        commBean.setCaiNum(0);
        commBean.setIsBest(false);
        dalClient.persist(commBean);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("topicId", commBean.getTopicId());
        // 话题参与人数+1
        if (VoteTopicRel.ViewPointConstants.VIEW_ZHENG.equals(commBean.getViewPoint())) {
            // 正方评论人数+1
            dalClient.execute("contend.incrZhengCommNum", map);
        } else {
            // 反方评论人数+1
            dalClient.execute("contend.incrFanCommNum", map);
        }
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<NmLibBean> getNmInfo() {
        Map<String, Object> map = new HashMap<String, Object>();
        return dalClient.queryForList("contend.getNmInfo", map, NmLibBean.class);
    }

    /**
     * 
     * 功能描述:修改用户头像 <br>
     * 〈功能详细描述〉
     *
     * @param person
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void modifyUserInfo(PersonBean person) {
        dalClient.dynamicMerge(person);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param personId
     * @param topicId
     * @param viewPoint
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Transactional
    public void topicVote(VoteTopicRel voteTopicRel) {
        // 新增话题与人员对应关系表
        dalClient.persist(voteTopicRel);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("topicId", voteTopicRel.getTopicId());
        // 话题参与人数+1
        if (VoteTopicRel.ViewPointConstants.VIEW_ZHENG.equals(voteTopicRel.getViewPoint())) {
            // 正方参与人数+1
            dalClient.execute("contend.incrZhengNum", map);
        } else {
            // 反方参与人数+1
            dalClient.execute("contend.incrFanNum", map);
        }

    }

    /**
     * 
     * 功能描述:踩赞评论 <br>
     * 〈功能详细描述〉
     *
     * @param commId
     * @param isZan
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void commVote(String commId, Boolean isZan) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commId", commId);
        if (isZan) {
            // 赞+1
            dalClient.execute("contend.incrZanNum", map);
        } else {
            // 踩+1
            dalClient.execute("contend.incrCaiNum", map);
        }

    }

    /**
     * 
     * 功能描述:删除评论 <br>
     * 〈功能详细描述〉
     *
     * @param commId
     * @param personId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void delComm(String commId, Long personId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("commId", commId);
        if (personId != null) {
            map.put("personId", personId);
        }
        dalClient.execute("contend.delComm", map);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param commId
     * @param personId
     * @param isBest
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void toBeBest(Long commId, Boolean isBest) {
        CommBean comm = new CommBean();
        comm.setCommId(commId);
        comm.setIsBest(isBest);
        comm.setZanNum(null);
        comm.setCaiNum(null);
        dalClient.dynamicMerge(comm);
    }
}
